package com.tech.arno.spicy.ui.activity;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tech.arno.spicy.R;
import com.tech.arno.spicy.ui.fragment.home.MessageFragment;
import com.tech.arno.spicy.ui.fragment.home.MineFragment;
import com.tech.arno.spicy.ui.fragment.home.NewsFragment;

/*
*   package:     com.tech.arno.spicy.ui.activity
*   fileName:    HomeActivityOld
*   author:    ArnoFrost
*   date:  2017/12/6 0006 16:43
*   desc:     首页Activity
*/
public class HomeActivityOld extends BaseActivity implements View.OnClickListener {
    /*
    *
    * */
    private FragmentManager fm;
    private NewsFragment mNewsFragment;
//    private Fragment mCommonFragmentOne;
    private MessageFragment mMessageFragment;
    private MineFragment mMineFragment;
    private Fragment mCurrent;


    private RelativeLayout mHomeLayout;
    private RelativeLayout mPondLayout;
    private RelativeLayout mMessageLayout;
    private RelativeLayout mMineLayout;

    private TextView mHomeView;
    private TextView mPondView;
    private TextView mMessageView;
    private TextView mMineView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_homeold_layout);
        //初始化
        initView();
        //添加Fragment
        mNewsFragment = new NewsFragment();
        fm = getFragmentManager();
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        fragmentTransaction.replace(R.id.content_layout, mNewsFragment);
        fragmentTransaction.commit();
    }

    private void initView() {
        mHomeLayout = (RelativeLayout) findViewById(R.id.home_layout_view);
        mHomeLayout.setOnClickListener(this);
        mPondLayout = (RelativeLayout) findViewById(R.id.pond_layout_view);
        mPondLayout.setOnClickListener(this);
        mMessageLayout = (RelativeLayout) findViewById(R.id.message_layout_view);
        mMessageLayout.setOnClickListener(this);
        mMineLayout = (RelativeLayout) findViewById(R.id.mine_layout_view);
        mMineLayout.setOnClickListener(this);

        mPondView = (TextView) findViewById(R.id.fish_image_view);
        mMessageView = (TextView) findViewById(R.id.message_image_view);
        mHomeView = (TextView) findViewById(R.id.home_image_view);
        mMineView = (TextView) findViewById(R.id.mine_image_view);
    }

    private void hideFragment(Fragment fragment, FragmentTransaction fragmentTransaction) {
        if (fragment != null) {
            fragmentTransaction.hide(fragment);
        }
    }

    @Override
    public void onClick(View v) {
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        switch (v.getId()) {
            case R.id.home_layout_view:
                mHomeView.setBackgroundResource(R.drawable.comui_tab_home_selected);
                mPondView.setBackgroundResource(R.drawable.comui_tab_pond);
                mMessageView.setBackgroundResource(R.drawable.comui_tab_message);
                mMineView.setBackgroundResource(R.drawable.comui_tab_person);

//                hideFragment(mCommonFragmentOne, fragmentTransaction);
                hideFragment(mMessageFragment, fragmentTransaction);
                hideFragment(mMineFragment, fragmentTransaction);
                if (mNewsFragment == null) {
                    mNewsFragment = new NewsFragment();
                    fragmentTransaction.add(R.id.content_layout, mNewsFragment);
                } else {
                    mCurrent = mNewsFragment;
                    fragmentTransaction.show(mNewsFragment);
                }
                break;
            case R.id.pond_layout_view:
//                mPondView.setBackgroundResource(R.drawable.comui_tab_pond_selected);
//                mHomeView.setBackgroundResource(R.drawable.comui_tab_home);
//                mMessageView.setBackgroundResource(R.drawable.comui_tab_message);
//                mMineView.setBackgroundResource(R.drawable.comui_tab_person);
//
//                hideFragment(mNewsFragment, fragmentTransaction);
//                hideFragment(mMessageFragment, fragmentTransaction);
//                hideFragment(mMineFragment, fragmentTransaction);
//                if (mCommonFragmentOne == null) {
//                    mCommonFragmentOne = new CommonFragment(1);
//                    fragmentTransaction.add(R.id.content_layout, mCommonFragmentOne);
//                } else {
//                    mCurrent = mCommonFragmentOne;
//                    fragmentTransaction.show(mCommonFragmentOne);
//                }
                break;
            case R.id.message_layout_view:
                mMessageView.setBackgroundResource(R.drawable.comui_tab_message_selected);
                mHomeView.setBackgroundResource(R.drawable.comui_tab_home);
                mPondView.setBackgroundResource(R.drawable.comui_tab_pond);
                mMineView.setBackgroundResource(R.drawable.comui_tab_person);

//                hideFragment(mCommonFragmentOne, fragmentTransaction);
                hideFragment(mNewsFragment, fragmentTransaction);
                hideFragment(mMineFragment, fragmentTransaction);
                if (mMessageFragment == null) {
                    mMessageFragment = new MessageFragment();
                    fragmentTransaction.add(R.id.content_layout, mMessageFragment);
                } else {
                    mCurrent = mMessageFragment;
                    fragmentTransaction.show(mMessageFragment);
                }
                break;
            case R.id.mine_layout_view:
                mMineView.setBackgroundResource(R.drawable.comui_tab_person_selected);
                mHomeView.setBackgroundResource(R.drawable.comui_tab_home);
                mPondView.setBackgroundResource(R.drawable.comui_tab_pond);
                mMessageView.setBackgroundResource(R.drawable.comui_tab_message);
//                hideFragment(mCommonFragmentOne, fragmentTransaction);
                hideFragment(mMessageFragment, fragmentTransaction);
                hideFragment(mNewsFragment, fragmentTransaction);
                if (mMineFragment == null) {
                    mMineFragment = new MineFragment();
                    fragmentTransaction.add(R.id.content_layout, mMineFragment);
                } else {
                    mCurrent = mMineFragment;
                    fragmentTransaction.show(mMineFragment);
                }
                break;
        }

        fragmentTransaction.commit();
    }


}
